package ecelib.db;

import java.sql.*;
import java.util.*;

//Class that handles all db requests regarding users
public class UserManager {
	static Connection con = null;
	static PreparedStatement addUser;

	private UserManager() {

	
	}

	private static void getConnection() {
		if (!Connector.isValidConnection(con)) {
			con = Connector.getConnection();
			if (!Connector.isValidConnection(con)) {
				throw new DBError("Cannot Create DB Connection");

			}
		}

	}

	public static boolean addNewUser(HashMap<String, String> values) {
		try {
			//setup statement
			getConnection();
			if(addUser==null || addUser.isClosed()){
				addUser = con
				.prepareStatement("INSERT INTO tblUser  (userID , userType, firstName,"
						+ "lastName , emailAddress, phoneNumber,password )  VALUES (?,?,?,?,?,?,?)");

			}
			addUser.clearParameters();
			
			
			// set values
			addUser.setInt(1, Integer.parseInt(values.get("userID")));
			addUser.setString(2, values.get("userType"));
			addUser.setString(3, values.get("firstName"));
			addUser.setString(4, values.get("lastName"));
			addUser.setString(5, values.get("emailAddress"));
			addUser.setString(6, values.get("phoneNumber"));
			addUser.setString(7, values.get("password"));
			int returnVal = addUser.executeUpdate();
			// TODO: Check result Set
			return true;

		} catch (SQLException e) {
			throw new DBError(e);

		}

	}

}
